#This code is used to generate the gromacs input file on windows system
import os
import os.path
import shutil

group = [
"sulfur",
"small_molecules",
"siloxanes",
#"silanes",
#"noble_gas",
"nitriles",
"fluoro",
"branched",
"benzenes",
"amine",
"amides",
"alkene",
"alkane",
"aldehydes",
"alcohols",
"acid",
"acetylene",
]
PPF_FILE = "AMBER3.ppf"
MSD_FOLDER = {}

def get_exp_info(file ,folder):
    #this function is not necessary, Just temporarily handle the exp information
    current_dir = os.getcwd()
    folder = os.path.join(current_dir, folder)
    
    expfile = os.listdir(folder)
    for i in expfile:
        if i[-3:] == "exp":
            print i
            f = open(os.path.join(folder,i), 'r')
            for j in f:
                file.write("%s    "%folder.split("\\")[-1] + j)
            f.close()

def get_msd_info(folder):
    #this function is not necessary, Just temporarily handle the msd information
    current_dir = os.getcwd()
    folder = os.path.join(current_dir, folder)
    
    file_list = []
    msdfile = os.listdir(folder)
    for i in msdfile:
        if i[-3:] == "msd":
            MSD_FOLDER[i] = folder
def get_exp_temperature():
    f = open("exp_data.log", 'r')
    exp_temperature = {}
    for i in f:
        a = i.split()
        exp_temperature[a[1]] = a[2]
    return exp_temperature
    
def mdp_file_input(folder, msd_file_name):
    run_temperature = get_exp_temperature()
    if msd_file_name in run_temperature.keys():
        t = run_temperature[msd_file_name]
        print msd_file_name, t
    
        file = open("run.mdp", "rt")
        template = file.read()
        file.close()
        template = template[:]
        template = template.replace("%RUN_STEPS%", "100000")
        template = template.replace("%RUN_TEMPERATURE%", t)
        path_out = os.path.join(folder, "equil.mdp")
        file = open(path_out, "wt")
        file.write(template)
        file.close()
    
        file = open("run.mdp", "rt")
        template = file.read()
        file.close()
        template = template[:]
        template = template.replace("%RUN_STEPS%", "200000")
        template = template.replace("%RUN_TEMPERATURE%", t)
        path_out = os.path.join(folder, "run.mdp")
        file = open(path_out, "wt")
        file.write(template)
        file.close()
    
        shutil.copy("em.mdp", folder)
    else:
        print "error in  " + msd_file_name
        
def sh_file_input(folder, msd_file_name):
    file = open("grompp.sh", "rt")
    template = file.read()
    file.close()
    template = template[:]
    template = template.replace("%MSD_FILE_NAME%", msd_file_name)
    template = template.replace("%NODES%", "8")
    path_out = os.path.join(folder, "grompp.sh")
    file = open(path_out, "wt")
    file.write(template)
    file.close()
    
def dff_2_gmx(folder):
    current_dir = os.getcwd()
    folder = os.path.join(current_dir, folder)
    
    file_list = []
    msdfile = os.listdir(folder)
    for i in msdfile:
        if i[-3:] == "msd":
            file_list.append(i)
    for i in file_list:
        #This part should be improved
        msd_file = os.path.join(folder, i)
        os.system("dff2gmx.jar %s %s %s"%(msd_file, PPF_FILE, i[:-4]))
        msd_file_name = i[:-4]
        msd_dir = os.path.join(folder, msd_file_name)
        if os.path.exists(msd_dir):
            pass
        else:
            os.mkdir(msd_dir)
        mdp_file_input(msd_dir, msd_file_name)
        sh_file_input(msd_dir, msd_file_name)
        shutil.copy(msd_dir+".itp", msd_dir)
        shutil.copy(msd_dir+".top", msd_dir)
        shutil.copy(msd_dir+".gro", msd_dir)

if __name__ == "__main__":
    for i in group:
        dff_2_gmx(i)
